import Antd, {
    message
} from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';

const validator = function (_rule, value) {
    return value
}

message.config({
    maxCount: 1,
});

export const _patternRules = {
    isAccount: {
        pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$ || [\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
        message: "账号格式不正确,只能是邮箱或手机号",
        validator
    },
    isMobile: {
        pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
        message: "手机号码格式不正确",
        validator
    },
    isEmail: {
        pattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
        message: "电子邮箱格式正不确",
        validator
    },
    isPassword: {
        pattern: /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/,
        message: "最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符",
        validator
    },
    isAffirmPassword: {
        pattern: /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/,
        message: "请输入正确的密码",
        validator
    }
}

export let isMobile = function (_rule, value) {
    let pattern = _patternRules.isMobile.pattern
    return pattern.test(value)
}
export let isEmail = function (_rule, value) {
    let pattern = _patternRules.isMobile.pattern
    return pattern.test(value)
}
export let isPassword = function (_rule, value) {
    let pattern = _patternRules.isPassword.pattern
    return pattern.test(value)
}

export let account = [{
    required: true,
    pattern: _patternRules.isAccount.pattern,
    message: _patternRules.isAccount.message,
    trigger: ['change']
}]
export let Email = [{
    required: true,
    pattern: _patternRules.isEmail.pattern,
    message: _patternRules.isEmail.message,
    trigger: ['change']
}]
export let password = [{
    required: true,
    pattern: _patternRules.isPassword.pattern,
    message: _patternRules.isPassword.message,
    trigger: ['change']
}]

export function rules(params = {}) {
    let account = [{
        required: true,
        pattern: _patternRules.isEmail.pattern,
        message: _patternRules.isEmail.message,
        trigger: ['change']
    }]
    let password = [{
        required: true,
        pattern: _patternRules.isPassword.pattern,
        message: _patternRules.isPassword.message,
        trigger: ['change']
    }]
    let affirmPassword = [{
        required: true,
        validator: async (_rule, value) => {
            if (!params.password) return Promise.reject('密码匹配参数必须为 password');
            if (value === '') return Promise.reject('确认密码不能为空');
            if (value !== params.password) return Promise.reject('确认密码和设置密码不匹配');
            return Promise.resolve()
        }
    }]
    let emailCode = [{
        required: true,
        message: "邮箱验证码不能为空"
    }]

    return {
        account,
        password,
        affirmPassword,
        emailCode
    }
}

export function success(msg, ...args) {
    if(typeof msg === "object") {
        message.success(msg);
    }else{
        if (args.length == 1) {
            if (typeof args[0] === "function")
                message.success(msg, 1, args[0]);
        } else {
            message.success(msg, args[0], args[1]);
        }
    }
}

export function warning(msg, ...args) {
    if(typeof msg === "object") {
        message.warning(msg);
    }else{
        if (args.length == 1) {
            if (typeof args[0] === "function")
                message.warning(msg, 1, args[0]);
        } else {
            message.warning(msg, args[0], args[1]);
        }
    }
}

export function hint(msg,type="open", ...args) {
    if(typeof msg === "object") {
        message[type](msg);
    }else{
        if (args.length == 1) {
            if (typeof args[0] === "function")
            message[type](msg, 1, args[0]);
        } else {
            message[type](msg, args[0], args[1]);
        }
    }
}


export default Antd